<template>
  <div class="app-container">
    <el-tabs :active-name="activeClientType" v-model="activeClientType" @tab-click="handleClientType">
      <el-tab-pane :label="item.label+'('+item.value+')'" :name="item.name" v-for="item in clientTypeList"
                   :key="item.name"
      ></el-tab-pane>
    </el-tabs>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="录入时间">
        <el-date-picker
          v-model=dateRange
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['enterprise:cluePool:add']"
        >录入业主
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-tabs :active-name="activeCluePoolState" v-model="activeCluePoolState" @tab-click="handleActiveCluePoolState">
      <el-tab-pane :label="item.label+'('+item.value+')'" :name="item.name" v-for="item in statusList" :key="item.name"
      ></el-tab-pane>
    </el-tabs>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column label="序号" width="55" align="center">
        <template slot-scope="scope">{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="userName"/>
      <el-table-column label="电话" align="center" prop="userPhone"/>
      <el-table-column label="房源地址" align="center" prop="premisesAddress">
        <template slot-scope="scope">
          <span>{{ scope.row.premisesAddress ? scope.row.premisesAddress : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="期望租金" align="center" prop="premisesAddress">
        <template slot-scope="scope">
          <span v-if="scope.row.minimumPrice">{{ scope.row.minimumPrice }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="房间数量" align="center" key="houseNumberType" prop="houseNumberType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.house_number_type" :value="scope.row.houseNumberType"/>
        </template>
      </el-table-column>
      <el-table-column label="户型" align="center" key="houseType" prop="houseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.house_type" :value="scope.row.houseType" v-if="scope.row.houseType"/>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="面积" align="center" prop="houseArea">
        <template slot-scope="scope">
          <span v-if="scope.row.houseArea">{{ scope.row.houseArea }}平米</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="最新跟进时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.updateTime">{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="录入人" align="center" prop="createBy"/>
      <el-table-column label="录入时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="cluePoolState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.clue_pool_state" :value="scope.row.cluePoolState"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="handleDetail(scope.row)"
            v-hasPermi="['enterprise:cluePool:query']"
          >
            详情
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="handleEdit(scope.row)"
            v-hasPermi="['enterprise:cluePool:edit']"
          >
            {{ scope.row.cluePoolState === '1' ? '编辑' : '激活' }}
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="handleCloseCluePool(scope.row)"
            v-if="(scope.row.cluePoolState === '1'&&queryParams.roleType === '1')||(activeClientType === '2'&&scope.row.cluePoolState === '1')"
            v-hasPermi="['enterprise:cluePool:close']"
          >
            关闭
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="handleClaimCluePool(scope.row)"
            v-if="scope.row.cluePoolState === '1'&&queryParams.roleType === '2'&&activeClientType === '1'"
            v-hasPermi="['enterprise:cluePool:close']"
          >
            认领
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="handleTransferToPublic(scope.row)"
            v-if="activeClientType === '2'"
          >
            转为公盘
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--  录入  -->
    <add :show.sync="open" :data="form" :title="title" @success="getList" user-type="2" :isSalesman="isSalesman" :isDetail="isDetail"/>
    <!--    关闭线索池弹窗-->
    <close-clue-pool :show.sync="closeDialog" :data="form" @success="getList"/>
  </div>
</template>

<script>
import {
  claimCluePool,
  countCluePoolStatus,
  countCluePoolType,
  listCluePoolByEnterprise
} from '@/api/cluePool/cluePool'
import add from '@/views/cluePool/proprietor/components/add.vue'
import closeCluePool from '@/views/cluePool/proprietor/components/closeClubPool.vue'

export default {
  name: 'adminClubPool',
  dicts: ['push_channel', 'clue_pool_state', 'house_type', 'house_number_type'],
  components: {
    add,
    closeCluePool
  },
  data() {
    return {
      // 选中客户类型
      activeClientType: '1',
      // 选中线索池状态
      activeCluePoolState: '0',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      detail: {},
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否销售员
      isSalesman: false,
      // 是否详情
      isDetail: false,
      // 关闭线索池弹窗
      closeDialog: false,
      // 日期范围
      dateRange: [],
      // 客户类型列表
      clientTypeList: [
        { label: '公盘', name: '1', value: '0' },
        { label: '私盘', name: '2', value: '0' }
      ],
      // 状态列表
      statusList: [
        { label: '全部', name: '0', value: '0' },
        { label: '正常', name: '1', value: '0' },
        { label: '关闭', name: '2', value: '0' }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cluePoolState: '',//线索池状态：1.正常、2.关闭（字典：clue_pool_state）
        clientType: '1',//客户类型：1.公客、2.私客
        userType: '2',//用户类型：1.租客、2.业主
        userName: '',//用户姓名
        userPhone: '',//用户手机号
        roleType: '1'//角色类型:1.管理员；2.销售员
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: '用户id不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  created() {
    console.log(this.$route)
    if (this.$route.name === 'SalesmanClubPool') {
      this.isSalesman = true
      this.queryParams.roleType = '2'//角色类型:1.管理员；2.销售员
    } else if (this.$route.name === 'AdminClubPool') {
      this.isSalesman = false
      this.queryParams.roleType = '1'//角色类型:1.管理员；2.销售员
    }
    this.init()
  },
  methods: {
    /** 初始化 */
    init() {
      this.getList()
      this.getCountCluePoolType()
    },
    // 客户类型切换
    handleClientType(tab) {
      console.log('handleClientType', tab.name)
      this.queryParams.clientType = tab.name
      this.handleQuery()
    },
    // 线索池状态切换
    handleActiveCluePoolState(tab) {
      console.log('handleActiveCluePoolState', tab.name)
      if (tab.name === '0') {
        this.queryParams.cluePoolState = ''
      } else {
        this.queryParams.cluePoolState = tab.name
      }
      this.handleQuery()
    },
    // 获取状态数量
    getCountCluePoolType() {
      let params = {
        roleType: this.queryParams.roleType,
        userType: '2'//用户类型：1.租客、2.业主
      }
      countCluePoolType(params).then(response => {
        this.clientTypeList[0].value = response.data['1']
        this.clientTypeList[1].value = response.data['2']
      })
    },
    // 获取状态数量
    getCountCluePoolStatus() {
      let params = {
        clientType: this.queryParams.clientType,
        roleType: this.queryParams.roleType,
        userType: '2'//用户类型：1.租客、2.业主
      }
      countCluePoolStatus(params).then(response => {
        this.statusList[0].value = response.data['1'] + response.data['2']
        this.statusList[1].value = response.data['1']
        this.statusList[2].value = response.data['2']
      })
    },
    /** 查询推送记录信息列表 */
    getList() {
      this.loading = true
      listCluePoolByEnterprise(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
      this.getCountCluePoolStatus()
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        pushRecordId: null,
        tenantId: null,
        tenantName: null,
        userName: null,
        templateCode: null,
        pushChannel: null,
        pushConditionCode: null,
        pushResult: null,
        messageHeader: null,
        messageContent: null,
        triggeringBehavior: null,
        pushTime: null,
        userId: null,
        recipient: null,
        businessId: null,
        messageState: null,
        messageType: null,
        createBy: null,
        updateBy: null,
        operatorId: null,
        createTime: null,
        updateTime: null,
        remark: null,
        triggeringTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pushRecordId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.isDetail = false
      this.title = '录入业主'
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pushRecordIds = row.pushRecordId || this.ids
      this.$modal.confirm('是否确认删除此数据项？').then(function() {
        return delPushRecord(pushRecordIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/pushRecord/export', {
        ...this.queryParams
      }, `pushRecord_${new Date().getTime()}.xlsx`)
    },
    /** 编辑/激活按钮操作 */
    handleEdit(row) {
      this.form = { ...row }
      this.title = '编辑/激活'
      this.isDetail = false
      this.open = true
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.form = { ...row }
      this.title = '详情'
      this.isDetail = true
      this.open = true
    },
    // 关闭线索池
    handleCloseCluePool(row) {
      this.form = { ...row }
      this.closeDialog = true
    },
    // 转为公盘
    handleTransferToPublic(row) {
      this.$modal.confirm('确定要把客源转为公盘吗？').then(() => {
        let params = {
          cluePoolId: row.cluePoolId
        }
        return claimCluePool(params)
      }).then((response) => {
        this.$modal.msgSuccess(response.msg || '转为公盘成功')
        this.init()
      }).catch(() => {
      })
    },
    // 认领线索池
    handleClaimCluePool(row) {
      this.$modal.confirm('确定认领到你的私盘池？').then(() => {
        let params = {
          cluePoolId: row.cluePoolId
        }
        return claimCluePool(params)
      }).then((response) => {
        this.$modal.msgSuccess(response.msg || '认领成功')
        this.init()
      }).catch(() => {
      })
    }
  }
}
</script>
<style lang="scss" scoped>

.content {
  .item {
    display: flex;
    padding: 10px 0;

    .label {
      width: 100px;
      font-weight: bold;

      &.dot {
        &:before {
          content: "*";
          color: black;

        }
      }
    }

    .value {
      .value-time {
        display: flex;
        align-items: baseline;
      }
    }
  }
}


</style>
